package com.itheima.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.GoodsMapper;
import com.itheima.pojo.ResultPageBean;
import com.itheima.pojo.Sku;
import com.itheima.service.GoodsService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Sku> implements GoodsService {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public void add(Sku sku) {
        Map<String,Object>map = new HashMap<>();
        map.put("type","save");
        map.put("data",sku);


        rabbitTemplate.convertAndSend("GoodsExchange","GoodsRoutingKey",map);
        this.save(sku);
    }

    @Override
    public void updateByID(Sku sku) {
        Map<String,Object>map = new HashMap<>();
        map.put("type","update");
        map.put("data",sku);


        rabbitTemplate.convertAndSend("GoodsExchange","GoodsRoutingKey",map);
        this.updateById(sku);
    }

    @Override
    public void deleteById(String id) {
        Map<String,Object>map = new HashMap<>();
        map.put("type","delete");
        map.put("data",id);


        rabbitTemplate.convertAndSend("GoodsExchange","GoodsRoutingKey",map);

        this.removeById(id);
    }

    @Override
    public Sku selectById(String id) {

       return this.getById(id);
    }

    @Override
    public ResultPageBean findByPage(int pageSize, int currentPage) {
        Page<Sku> skuPage = new Page<>(currentPage, pageSize);
        Page<Sku> page = this.page(skuPage);
        long total = page.getTotal();
        List<Sku> list = page.getRecords();
       ResultPageBean resultPageBean = new  ResultPageBean();
       resultPageBean.setLists(list);
       resultPageBean.setTotal(total);
       return resultPageBean;

    }

    @Override
    public Integer getTotal() {
      return   baseMapper.getTotal();
    }


}
